#ifndef ZDT_UART_H
#define ZDT_UART_H

#include "bsp_usart.h"
#include "daemon.h"
#include "stdbool.h"
#include "motor_def.h"

#define		ABS(x)		((x) > 0 ? (x) : -(x))
#define 	DIR(x)		((x) > 0 ? (CW): (CCW))

typedef enum
{
	ZDT_ERROR = 0x00,
	ZDT_PULSE = 0x32,
	ZDT_VEL = 0x35,
	ZDT_CPOS = 0x36,
} ZDTCommand_t;

typedef enum
{
	ZDT_OK,
	ZDT_ID_ERROR,
	ZDT_COMMAND_ERROR
}ZDTReturn_t;

typedef enum
{
	CW = 0,
	CCW = 1
}ZDTDir_e;

typedef enum
{
	S_VER = 0, 
	S_RL = 1, 
	S_PID = 2, 
	S_VBUS = 3, 
	S_CPHA = 5, 
	S_ENCL = 7, 
	S_TPOS = 8, 
	S_VEL = 9, 
	S_CPOS = 10, 
	S_PERR = 11, 
	S_FLAG = 13, 
	S_Conf = 14, 
	S_State = 15, 
	S_ORG = 16, 
	S_PULSE =17,
} SysParams_t;

typedef struct
{
	int32_t Pulse;
	int32_t CurrentPosition;
	int16_t CurrentSpeed;

    //转化后的
    float angle, last_angle;
    float speed;
    float torque;
	float rad, last_rad;
} ZDTState_t;

typedef struct
{
	float x, y, error_x, error_y, s;
	float temp_x, temp_y, last_temp_x, last_temp_y;
} Displacement;

typedef struct 
{
	USARTInstance *motor_usart_instance;
	DaemonInstance* motor_daemon;
	uint8_t id;
	ZDTState_t state;
	uint8_t received_flag;
	Displacement displacement;
} ZDTInstance;

typedef struct
{
	ZDTInstance *instance;
	SysParams_t Param;
} ZDTSend;



typedef struct
{
	uint8_t id;
} ZDT_Motor_Init_Config_s;

void ZDT_Angle_Calc(ZDTInstance *zdt, int x);
void ZDT_Vel_Send(ZDTInstance *zdt, int16_t vel, uint8_t acc);
void ZDT_Pos_Control(ZDTInstance *zdt, uint8_t dir, uint16_t vel, uint8_t acc, uint32_t clk, bool raF, bool snF);
void ZDT_En_Control(ZDTInstance *zdt, bool state, bool snF);
void ZDT_Reset_CurPos_To_Zero(ZDTInstance *zdt);
void ZDT_Read_Sys_Params(ZDTInstance *zdt, SysParams_t s);
ZDTReturn_t ZDT_UnPack(uint8_t data[]);
ZDTInstance *ZDTMotorUSARTInit(UART_HandleTypeDef *zdt_usart_handle, ZDT_Motor_Init_Config_s config);

#endif